package com.c1202m.gui;

import com.c1202m.dao.DangNhapDAO;
import com.c1202m.dao.BangGiaoDichDAO;
import com.c1202m.dao.RutTienDAO;
import com.c1202m.util.KiemTraDuLieu;
import com.c1202m.dao.TaiKhoanDAO;
import com.c1202m.hibernate.entities.TaiKhoan;
import java.awt.Image;
import java.text.NumberFormat;
import java.util.Date;
import java.util.Locale;
import javax.swing.text.JTextComponent;

public class RutTien extends javax.swing.JDialog {

    private String selectedSoTK;
    private int soLanRutN = 0;
    private double soTienRutTrongNgay = 0;

    public RutTien(java.awt.Frame parent, boolean modal, String selectedID) {
        super(parent, modal);
        initComponents();
        Image icon = getToolkit().getImage(getClass().getResource("/images/app_icon_32.png"));
        setIconImage(icon);
        setLocationRelativeTo(DangNhapDAO.as);
        setVisible(true);
        this.selectedSoTK = selectedID;
        fillFields();
    }

    private void fillFields() {
        TaiKhoan danhSachTK = BangGiaoDichDAO.layMotDoiTuong(selectedSoTK);
        soLanRutN = RutTienDAO.soGiaoDichTrongNgay(selectedSoTK, new Date());
        soTienRutTrongNgay = RutTienDAO.soTienRutTrongNgay(selectedSoTK, new Date());
        txtSoTK.setText(String.valueOf(danhSachTK.getSoTK()));
        Locale loc = new Locale("vi", "VN");
        NumberFormat nf = NumberFormat.getCurrencyInstance(loc);
        txtSoDuTK.setText(nf.format(Double.valueOf(danhSachTK.getSoDuTK())));
        txtSoLanRut.setText(String.valueOf(soLanRutN));
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        txtSoTK = new javax.swing.JTextField();
        txtSoDuTK = new javax.swing.JTextField();
        txtSoLanRut = new javax.swing.JTextField();
        txtSoTienRut = new javax.swing.JTextField();
        btnUpdate = new javax.swing.JButton();
        btnExit = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Sua Quan Tri");
        setBounds(new java.awt.Rectangle(0, 0, 0, 0));
        setResizable(false);

        jPanel1.setBackground(new java.awt.Color(255, 255, 255));
        jPanel1.setPreferredSize(new java.awt.Dimension(405, 300));

        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/lebel_ruttien.png"))); // NOI18N
        jLabel1.setPreferredSize(new java.awt.Dimension(405, 100));

        jLabel2.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel2.setForeground(new java.awt.Color(51, 51, 51));
        jLabel2.setText("Số tài khoản");
        jLabel2.setPreferredSize(new java.awt.Dimension(140, 20));

        jLabel3.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel3.setForeground(new java.awt.Color(51, 51, 51));
        jLabel3.setText("Số dư tài khoản");
        jLabel3.setPreferredSize(new java.awt.Dimension(140, 20));

        jLabel4.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel4.setForeground(new java.awt.Color(51, 51, 51));
        jLabel4.setText("Số tiền rút");
        jLabel4.setPreferredSize(new java.awt.Dimension(140, 20));

        jLabel5.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel5.setForeground(new java.awt.Color(51, 51, 51));
        jLabel5.setText("Số lần đã rút trong ngày");
        jLabel5.setPreferredSize(new java.awt.Dimension(140, 20));

        txtSoTK.setEditable(false);
        txtSoTK.setBackground(new java.awt.Color(255, 255, 255));
        txtSoTK.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        txtSoTK.setForeground(new java.awt.Color(255, 51, 0));
        txtSoTK.setPreferredSize(new java.awt.Dimension(200, 20));

        txtSoDuTK.setEditable(false);
        txtSoDuTK.setBackground(new java.awt.Color(255, 255, 255));
        txtSoDuTK.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        txtSoDuTK.setForeground(new java.awt.Color(255, 0, 0));
        txtSoDuTK.setPreferredSize(new java.awt.Dimension(200, 20));

        txtSoLanRut.setEditable(false);
        txtSoLanRut.setBackground(new java.awt.Color(255, 255, 255));
        txtSoLanRut.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        txtSoLanRut.setForeground(new java.awt.Color(255, 0, 0));
        txtSoLanRut.setPreferredSize(new java.awt.Dimension(200, 20));

        txtSoTienRut.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        txtSoTienRut.setForeground(new java.awt.Color(0, 0, 255));
        txtSoTienRut.setToolTipText("Số tiền rút ít nhất trong một lần giao dịch là "+Help.rutToiThieuGD+"VND, và số tiền rút nhiều nhất trong một lần giao dịch là "+Help.rutToiDaGD+"VND");
        txtSoTienRut.setPreferredSize(new java.awt.Dimension(200, 20));

        btnUpdate.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/icon-withdraw.png"))); // NOI18N
        btnUpdate.setText("Rút");
        btnUpdate.setMargin(new java.awt.Insets(2, 10, 2, 10));
        btnUpdate.setPreferredSize(new java.awt.Dimension(75, 30));
        btnUpdate.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnUpdateActionPerformed(evt);
            }
        });

        btnExit.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/exit.png"))); // NOI18N
        btnExit.setText("Hủy");
        btnExit.setPreferredSize(new java.awt.Dimension(75, 30));
        btnExit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnExitActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(85, 85, 85)
                .addComponent(btnUpdate, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(85, 85, 85)
                .addComponent(btnExit, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(20, 20, 20)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(25, 25, 25)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(txtSoLanRut, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtSoTK, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtSoDuTK, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtSoTienRut, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(30, 30, 30)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtSoTK, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(10, 10, 10)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtSoDuTK, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(10, 10, 10)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtSoLanRut, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(10, 10, 10)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtSoTienRut, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(20, 20, 20)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnExit, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnUpdate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnExitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnExitActionPerformed
        dispose();
    }//GEN-LAST:event_btnExitActionPerformed

    private void btnUpdateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnUpdateActionPerformed
        JTextComponent[] tcp = {txtSoTK, txtSoTienRut};
        if (!KiemTraDuLieu.kiemTraTruongTrong(tcp)) {
            return;
        }
        double soTienRut = 0;
        if (!KiemTraDuLieu.isEmpty(txtSoTienRut.getText())) {
            if (!KiemTraDuLieu.kiemTraSo(txtSoTienRut)) {
                return;
            } else {
                soTienRut = Double.valueOf(txtSoTienRut.getText());
            }
        }

        if (soTienRut < Help.rutToiThieuGD || soTienRut > Help.rutToiDaGD) {
            new CustomMessageDialog(null, true, "Warning!", "<html>Rút tiền thất bại<br/>Số tiền rút không hợp lệ</html>", CustomMessageDialog.MESSAGE);
            return;
        } else if ((TaiKhoanDAO.layMotDoiTuong(selectedSoTK).getSoDuTK() < Help.soDuToiThieu)
                || (TaiKhoanDAO.layMotDoiTuong(selectedSoTK).getSoDuTK() - Help.soDuToiThieu < soTienRut)) {
            new CustomMessageDialog(null, true, "Warning!", "<html>Rút tiền thất bại<br/>Số dư tài khoản không đủ</html>", CustomMessageDialog.MESSAGE);
            return;
        } else if (soLanRutN >= Help.soLanRutToiDaN) {
            new CustomMessageDialog(null, true, "Warning!", "<html>Rút tiền thất bại<br/>Số lần rút tiền vượt quá giới hạn</html>", CustomMessageDialog.MESSAGE);
            dispose();
            return;
        } else if (soTienRutTrongNgay + soTienRut > Help.rutToiDaN) {
            new CustomMessageDialog(null, true, "Warning!", "<html>Rút tiền thất bại<br/>Số tiền rút trong ngày vượt quá giới hạn</html>", CustomMessageDialog.MESSAGE);
//            dispose();
            return;
        }
        if (RutTienDAO.rutTien(selectedSoTK, soTienRut)) {
            new CustomMessageDialog(null, true, "Done!", "Rút tiền thành công!", CustomMessageDialog.DONE);
            BangGiaoDich.lamMoiBangDanhSachTK();
            BangGiaoDich.lamMoiBangRutTien();
            BangTaiKhoan.lamMoiBangTK();
            dispose();
        }
    }//GEN-LAST:event_btnUpdateActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnExit;
    private javax.swing.JButton btnUpdate;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JTextField txtSoDuTK;
    private javax.swing.JTextField txtSoLanRut;
    private javax.swing.JTextField txtSoTK;
    private javax.swing.JTextField txtSoTienRut;
    // End of variables declaration//GEN-END:variables
}
